using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using IGlobalist.Cms.Common;
using System.Collections.Generic;
using IGlobalist.Cms.Model;
using IGlobalist.Cms.Bll;
using System.IO;

namespace IGlobalist.Cms.Admin.Control
{
    public partial class ResourceFileList : AdminBaseUserControl
    {
        public event EventHandler EventDataResoucesChange;
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        
        
        public void DoBindGridViewFile(object sender, EventArgs e)
        {   
            ResourceFolderTree fileTree = (ResourceFolderTree)sender;
            long ID = Convert.ToInt64(fileTree.SelectedValue);            
            if (ID > 0)
            {            
                Session[SessionVariable.Folder.CurrenttFolderID] = ID;
                DoBindGVFileList(ID);
                IResourceFolder folder =  ResourceFolderController.GetResourceFolder(ID);
                lblStatus.Text = "View resource file list of folder " + folder.Name;
            }
        }

        public void DoBindGridViewFileSession(object sender, EventArgs e)
        {   
            DoBindGVFileList(0);
            lblStatus.Text = "Insert Resource file successful";
        }
        public void DoBindGVFileList(long ID)
        {
            //lblStatus.Text = "View Resource file";
            if (ID == 0)
            {
                ID = Convert.ToInt64(Session[SessionVariable.Folder.CurrenttFolderID]);
                
            }
            IList<IResourceFile> dsResourceFile = ResourceFileController.GetResourceFileListByFolderID(ID);
            if (dsResourceFile != null)
            {
                gvFileList.DataSource = dsResourceFile;
                gvFileList.DataBind();
            }
        }

        public string SelectedValue
        {
            get { return (string)ViewState["_selectedFileID"]; }
            set { ViewState["_selectedFileID"] = value; }
        }
        protected void ibEdit_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton ib = (ImageButton)sender;
            SelectedValue = ib.CommandArgument;
            if (EventDataResoucesChange != null)
            {
                EventDataResoucesChange(this, e);
            }
        }
        public void DoShow(object sender, EventArgs e)
        {
            Visible = true;
        }
        public void DoHidden(object sender, EventArgs e)
        {
            Visible = false;
        }

        protected void ibDelete_Click(object sender, ImageClickEventArgs e)
        {
            ImageButton ibEdit = (ImageButton)sender;
            long ID = Convert.ToInt64(ibEdit.CommandArgument);
            if (ID > 0)
            {
                IResourceFile rf = ResourceFileController.GetResourceFile(ID);
                if (rf != null)
                {
                    string path = Path.Combine(DataRootPhysicalPath, "FileUpload");
                    string filename = Path.Combine(path,rf.ID+rf.Extention);
                    if(File.Exists(filename))
                    {
                        File.Delete(filename);
                    }
                }
                ResourceFileController.DeleteResourceFile(ID);
                lblStatus.Text = "Delete file successful";
            }
            DoBindGVFileList(0);
        }

        protected void gvFileList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow) { }
        }        
    }
}